﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraReports.UI;
namespace Exact_Extension
{
    public partial class RedInvoiceForm : DevExpress.XtraEditors.XtraForm
    {
        int LastRowHandle = 0;
        ExactDataContext DS;
        Common common;
        public RedInvoiceForm(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            
            common = new Common(DS);
        }
        private void LoadData()
        {
            var query = from TaxInv in DS.ISHCMC_V_TaxInvoices
                        //where TaxInv.Outstanding == 0 && TaxInv.TaxInvoiceName.Length >0
                        select TaxInv;
            List<ISHCMC_V_TaxInvoice> ListTaxInvoice = new List<ISHCMC_V_TaxInvoice>();
            foreach (ISHCMC_V_TaxInvoice itemInv in query)
            {
                itemInv.InvoiceDate = DateTime.Now;
                ListTaxInvoice.Add(itemInv);
            }
            this.iSHCMCVTaxInvoiceBindingSource.DataSource = ListTaxInvoice;
                        
        }
        private void gridView_TaxInvoice_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu1.ShowPopup(Control.MousePosition);

                LastRowHandle = e.RowHandle;

            }
        }

        private void btnUpdateVATNo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_TaxInvoice itemVTaxInvoice = (ISHCMC_V_TaxInvoice)this.gridView_TaxInvoice.GetRow(LastRowHandle);
            
            DialogResult result;
            if (itemVTaxInvoice.VATInvoiceNo != null && itemVTaxInvoice.VATInvoiceNo.Length > 0)
            {
                MsgUpdateVATInvoiceNo msg = new MsgUpdateVATInvoiceNo(itemVTaxInvoice, DS,1);
                result = msg.ShowDialog();
            }
            else
            {
                MsgUpdateVATInvoiceNo msg = new MsgUpdateVATInvoiceNo(itemVTaxInvoice, DS, 0);
                result = msg.ShowDialog();
            }
                if (result == DialogResult.OK)
                {
                    this.LoadData();
                    this.gridView_TaxInvoice.Columns["StudentID"].FilterInfo =
                            new ColumnFilterInfo("[StudentID] LIKE '" + itemVTaxInvoice.StudentID+ "'");
                }
            


        }

        private void btnShowAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView_TaxInvoice.ClearColumnsFilter();
        }

       
        private void btnPrintVATInvoice_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
            ISHCMC_V_TaxInvoice itemVTaxInvoice = (ISHCMC_V_TaxInvoice)this.gridView_TaxInvoice.GetRow(LastRowHandle);
            itemVTaxInvoice.AmountInWord = common.So_chu(itemVTaxInvoice.TotalAmount.Value, "đồng");
            RedInvoiceRpt rpt = new RedInvoiceRpt(itemVTaxInvoice);
            ReportPrintTool PrintTool = new ReportPrintTool(rpt);
            PrintTool.ShowPreview();
        }

        private void btnLoadData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadData();
        }

        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RootRpt rootrpt = new RootRpt();
            rootrpt.CreateDocument();
            foreach (int i in this.gridView_TaxInvoice.GetSelectedRows())
            {
                ISHCMC_V_TaxInvoice itemVTaxInvoice = (ISHCMC_V_TaxInvoice)this.gridView_TaxInvoice.GetRow(i);
                itemVTaxInvoice.AmountInWord = common.So_chu(itemVTaxInvoice.TotalAmount.Value, "đồng");
                RedInvoiceRpt rpt = new RedInvoiceRpt(itemVTaxInvoice);
                rpt.CreateDocument();
                rootrpt.Pages.AddRange(rpt.Pages);
               
            }
            ReportPrintTool PrintTool = new ReportPrintTool(rootrpt);
            PrintTool.ShowPreview();
        }

        private void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView_TaxInvoice.ShowPrintPreview();
        }

        private void btnUpdateTaxInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_TaxInvoice itemVTaxInvoice = (ISHCMC_V_TaxInvoice)this.gridView_TaxInvoice.GetRow(LastRowHandle);

            DialogResult result;

            MsgUpdateTaxInfo msg = new MsgUpdateTaxInfo(DS, itemVTaxInvoice);
                result = msg.ShowDialog();
         
            if (result == DialogResult.OK)
            {
                this.LoadData();
                this.gridView_TaxInvoice.Columns["StudentID"].FilterInfo =
                        new ColumnFilterInfo("[StudentID] LIKE '" + itemVTaxInvoice.StudentID + "'");
            }
        }

       

      
    }
}